package com.ryan.mms.privilage.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.ryan.dictionary.member.enums.OperatorStatusEnum;
import com.ryan.framework.dto.DtoUtil;
import com.ryan.framework.exception.AppException;
import com.ryan.framework.tag.pagination.Page;
import com.ryan.mms.privilage.dao.OperatorDao;
import com.ryan.mms.privilage.dto.OperatorDto;
import com.ryan.mms.privilage.dto.ResourceDto;
import com.ryan.mms.privilage.model.Operator;
import com.ryan.mms.privilage.model.Resource;
import com.ryan.mms.privilage.service.OperatorService;
import com.ryan.mms.privilage.service.ResourceService;

@Service
@SuppressWarnings("unchecked")
public class OperatorServiceImpl implements OperatorService<OperatorDto,Operator,Long>{
	
	@Autowired
	private OperatorDao<Operator,Integer> operatorDao;
	@SuppressWarnings("unused")
	private ResourceService<ResourceDto,Resource,Long> resourceService;
	private DtoUtil<OperatorDto,Operator> operatorDtoUtil = DtoUtil.getInstance(OperatorDto.class, Operator.class);
	
	public OperatorDto save(final OperatorDto dto) {
		Operator model = operatorDtoUtil.convertDto2Model(dto);
		operatorDao.save(model);
		OperatorDto result = (OperatorDto) operatorDtoUtil.convertModel2Dto(model);
        return result;
    }
    
    public OperatorDto update(final OperatorDto dto) {
        return save(dto);
    }
    
	public void delete(final OperatorDto dto) {
		Operator model = operatorDtoUtil.convertDto2Model(dto);
        operatorDao.delete(model);
    }

	public void deleteById(Long pk) {
		operatorDao.delete(pk);
    }

	public OperatorDto findById(Long pk) {
		Operator model = operatorDao.get(pk);
		OperatorDto result = operatorDtoUtil.convertModel2Dto(model);
        return result;
    }

	public List<OperatorDto> getAll() {
        List<Operator> models = operatorDao.getAll();
        List<OperatorDto> dtos = operatorDtoUtil.convertModels2Dtos(models);
        return dtos;
    }

	public List<OperatorDto> getAll(final Page page) {
        List<Operator> models = operatorDao.getAll(page);
		List<OperatorDto> dtos = operatorDtoUtil.convertModels2Dtos(models);
        return dtos;
    }

	public OperatorDto doInIsolatedTx(OperatorDto dto) {
		Operator model = operatorDtoUtil.convertDto2Model(dto);
		Operator savedModel = this.operatorDao.save(model);
		return operatorDtoUtil.convertModel2Dto(savedModel);
	}
	
	public OperatorDto login(OperatorDto userDto) {
		return operatorDtoUtil.convertModel2Dto(operatorDao.findByLoginNameAndPassword(operatorDtoUtil.convertDto2Model(userDto)));
	}


	public OperatorDto findByLoginName(String loginName) throws AppException {
		OperatorDto operatorDto = operatorDtoUtil.convertModel2Dto(operatorDao.findUniqueBy("loginName", loginName));
		if (operatorDto == null) {
			throw new AppException("该用户名<"+loginName+">不存在！");
		}
		if (OperatorStatusEnum.FREEZE.getKey().equals(operatorDto.getStatus())) {
			throw new AppException("该用户名暂停止使用！");
		}
		if (OperatorStatusEnum.LOCKED.getKey().equals(operatorDto.getStatus())) {
			throw new AppException("该用户多次登录失败,已被锁！");
		}
//		List<ResourceDto> resourceList = resourceService.getAllResourceByOperatorId(operatorDto.getOperatorId());
//		operatorDto.setResourceList(resourceList);
		return operatorDto;
	}

	public List<OperatorDto> find(Page page, OperatorDto operatorDto) {
        List<Operator> models = operatorDao.findByOperator(page,operatorDtoUtil.convertDto2Model(operatorDto));
		List<OperatorDto> dtos = operatorDtoUtil.convertModels2Dtos(models);
		return dtos;
	}


	public void deleteAll(List<Long> pks) {
		operatorDao.deleteByOperatorIds(pks);
	}


	public void setOperatorDao(OperatorDao<Operator, Integer> operatorDao) {
		this.operatorDao = operatorDao;
	}

	public void setResourceService(
			ResourceService<ResourceDto, Resource, Long> resourceService) {
		this.resourceService = resourceService;
	}
}
